import {ethers} from "ethers"

const ALCHEMY_MAINNET_URL = 'https://eth-sepolia.g.alchemy.com/v2/mkM5-GVJCs00ghRsjT7aU';

const provider = new ethers.JsonRpcProvider(ALCHEMY_MAINNET_URL);

// WETH ABI，只包含我们关心的Transfer事件
const abiWETH = [
    "event Transfer(address indexed from, address indexed to, uint amount)"
];
// WETH合约地址（sepolia 测试网）
const addressWETH = '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9' // WETH Contract

// 声明一个只读合约对象
const contractWETH = new ethers.Contract(addressWETH, abiWETH, provider)

// 获取当前blockNumber
const blockNumber = await provider.getBlockNumber()
console.log(`当前区块高度${blockNumber}`)

// 获取过去10个区块的Transfer事件
const transferEvents = await contractWETH.queryFilter('Transfer', blockNumber - 10, blockNumber)
console.log(transferEvents[0])

// 解析事件
const amount = ethers.formatUnits(ethers.getBigInt(transferEvents[0].args["amount"]), "ether");
console.log(`地址 ${transferEvents[0].args["from"]} 转账${amount} WETH 到地址 ${transferEvents[0].args["to"]}`)

